<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片区域 -->
      <el-card class="box-card">
        <!-- tabs区域 -->
        <el-tabs v-model="activeName">
          <el-tab-pane label="组织架构" name="first" class="tab-pane">
            <el-row class="department-header">
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex">
                  <el-col>负责人</el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addPatsFn('')">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-tree :data="treeData" :props="defaultProps" default-expand-all>
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="4">
                    <el-row type="flex">
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addPatsFn(data.id)">添加子部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editPatsFn(data.id)">编辑部门</el-dropdown-item>
                            <el-dropdown-item v-if="data && !data.children" @click.native="delPatsFn(data.id)">删除此部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
        <el-dialog
          :title="isEdit?'编辑':'添加'"
          :visible.sync="showDialog"
          width="50%"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
        >
          <!-- 数据回显,显示数据覆盖原因:子组件没有销毁,所有每次不会都created
        我们只需要在弹框出现的时候销毁子组件就可以 -->
          <dept-dialog v-if="showDialog" :origin-list="originList" :parent-id="parentId" :is-edit="isEdit" @add-list="addlistFn" @close="closeConfirm" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getdepartment, delDepartDetail } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import deptDialog from './deptDialog.vue'

export default {
  name: 'Departments',
  components: { deptDialog },
  data() {
    return {
      activeName: 'first',
      treeData: [], // 树形的数据
      defaultProps: { // 树形数组显示的属性
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      parentId: '', // id值
      isEdit: false, // 判断编辑或添加的标识
      originList: []

    }
  },
  created() {
    this.getdepartment()
  },
  methods: {
    // 取消按钮关闭弹窗事件
    closeConfirm() {
      this.showDialog = false
    },
    // 添加部门事件
    addlistFn() {
      // 关闭弹窗
      this.showDialog = false
      // 渲染数据
      this.getdepartment()
    },
    // 删除部门
    async delPatsFn(id) {
      // 弹框--给用户提示
      const tipRes = await this.$confirm('此操作将永久删除此部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (tipRes === 'cancel') {
        this.$message('您取消删除')
      } else {
        // 调用接口
        const res = await delDepartDetail(id)
        this.$message.success(res.message)
        // 渲染列表
        this.getdepartment()
      }

      // console.log(res)
    },
    // 添加子部门,往子组件传值事件
    addPatsFn(id) {
      this.showDialog = true
      this.parentId = id
      this.isEdit = false
    },
    // 编辑子部门,往子组件传值事件
    editPatsFn(id) {
      this.showDialog = true
      this.parentId = id
      this.isEdit = true
    },
    // 获取组织架构数据
    async getdepartment() {
      // 调用API
      const res = await getdepartment()
      // console.log(res)
      // 精简需要的数组
      this.originList = res.data.depts.map(item => ({
        name: item.name,
        id: item.id,
        code: item.code,
        pid: item.pid
      }))
      this.treeData = tranListToTreeData(res.data.depts)
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
